package com.F09.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.F09.actionform.F0901UserActionForm;
import com.F09.dao.OrgUserDAO;
import com.action.BaseAction;
import com.config.Constants;
import com.info.UserInfo;
import com.util.BuildMessages;
import com.util.CommonTools;

/**
 * 
 * @file F0901UserAction.java
 * @author Liushimin
 * @since 2008-10-14
 * @version 1.0
 */
public class F0901UserAction extends BaseAction {

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @�޸� by liusm  2008-10-09
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		F0901UserActionForm actionForm = (F0901UserActionForm) form;
		String orgName = actionForm.getORGNAME();
		String orgname2 =request.getParameter("ORGNAME");
		System.out.println("orgName is = "+orgName);
		System.out.println("orgname2 is = "+orgname2);
		String strGet = "GET";
		String strPost = "POST";
		String strCharEncodingISO = "ISO-8859-1"; 
		String strCharEncodingGBK = "GBK";
		String strCharEncodingUTF = "UTF-8";
		String strSubmit = "submit";
		String strTrue = "true";
		String strFalse = "false";
		String strQuery = "query";
		String strList = "list";
		String strDetail = "detail";
		String strRole = "role";
		String strAddRole = "addRole";
		String strSubmitAdd = "submitAdd";
		String strAddLeft = "addLeft";
		String strAddRight = "addRight";
		String strType = "11";
		
		try{
			if(request.getMethod().equals(strGet)){
//				orgName = new String(orgName.getBytes(strCharEncodingUTF),strCharEncodingGBK);
				System.out.println("GET--------------------------"+orgName);
			}else if(request.getMethod().equals(strPost)){
				request.setCharacterEncoding(strCharEncodingUTF);
				System.out.println("POST--------------------------"+orgName);
			}
		}catch(UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		OrgUserDAO dao = new OrgUserDAO();
		UserInfo info = new UserInfo();
		String operationType = actionForm.getOperationType();
		
		orgName = this.getNameById(actionForm.getORGID(),dao);
		actionForm.setORGNAME(orgName);
		if(CommonTools.isNullString(actionForm.getSearchState())){
			actionForm.setSearchState(Constants.STATE_01);
		}
		try {
			if(strSubmit.equals(operationType)){
				info.setUserId(actionForm.getUSERID());
				info.setCode(actionForm.getCODE());
				info.setName(actionForm.getNAME());
				info.setPassword(actionForm.getPASSWORD());		
				info.setOrgId(actionForm.getORGID());	
				info.setManagOrgId(actionForm.getMANAGORGID());
				info.setTel(actionForm.getTEL());				
				info.setMobil(actionForm.getMOBIL());		
				info.setNotes(actionForm.getNOTES());
				if(strTrue.equals((String)actionForm.getFlag())){
					info.setIP(actionForm.getIP1()+"."+actionForm.getIP2()+"."+actionForm.getIP3()+"."+actionForm.getIP4()+"."+actionForm.getIP5());	
				}else{
					info.setIP("");
				}
				List list = new ArrayList();
				list = dao.selectByUserCode(info);
				try{
				if(list.isEmpty()){ //list == null || list.size() == 0 
					if("".equals(actionForm.getUSERID())){	
						dao.insertUser(info);	
					}
					if(!("".equals(actionForm.getUSERID()))){	
						dao.updateUser(info);
					}
					actionForm.setErrorFlag("");
					operationType=strQuery;
					}else {	
						super.saveMessages(request, BuildMessages.build("common.message"));
						return mapping.findForward(strDetail);
					}
				}catch(Exception e){
					actionForm.setErrorFlag("");
					e.printStackTrace();
				}
			}
			
			if(strSubmitAdd.equals(operationType)){
				
					if(strAddRight.equals(actionForm.getType())){
						try{
							dao.insertUserRole(CommonTools.array2String((String [])actionForm.getArrayID()),actionForm.getUSERID());
							//actionForm.setErrorFlag("");
						}catch(Exception e){
							//actionForm.setErrorFlag("");
							e.printStackTrace();
						}
					}
					if(strAddLeft.equals(actionForm.getType())){
						try{
							dao.deleteByUserRole(CommonTools.array2String((String [])actionForm.getArrayID()),actionForm.getUSERID());
							//actionForm.setErrorFlag("");
						}catch(Exception e){
							//actionForm.setErrorFlag("");
							e.printStackTrace();
						}
					}
					operationType=strRole;			
			}
			if(Constants.ADD.equals(operationType)){
				actionForm.setUSERID("");
				actionForm.setCODE("");
				actionForm.setNAME("");
				actionForm.setPASSWORD("");		
				actionForm.setCONPASSWORD("");	
				actionForm.setMANAGORGID("");	
				actionForm.setMANAGORGNAME("");
				actionForm.setTEL("");				
				actionForm.setMOBIL("");			
				actionForm.setNOTES("");
				actionForm.setIP1("");
				actionForm.setIP2("");
				actionForm.setIP3("");
				actionForm.setIP4("");
				actionForm.setIP5("");
				actionForm.setORGNAME(actionForm.getORGNAME());
				return mapping.findForward(strDetail);
			}
			if(Constants.EDIT.equals(operationType)){ 
				Map map = null;
				List list = new ArrayList();
				list = dao.selectByUserId(actionForm.getUSERID());
				 for(int i = 0; i < list.size();i++){
					map = (Map)list.get(i);	
					actionForm.setORGID(CommonTools.null2String((String)map.get("ORGID")));
					actionForm.setUSERID(CommonTools.null2String((String)map.get("USERID")));
					actionForm.setCODE(CommonTools.null2String((String)map.get("CODE")));
					actionForm.setNAME(CommonTools.null2String((String)map.get("NAME")));
					actionForm.setPASSWORD(CommonTools.null2String((String)map.get("PASSWORD")));
					actionForm.setCONPASSWORD(CommonTools.null2String((String)map.get("PASSWORD")));
					actionForm.setMANAGORGID(CommonTools.null2String((String)map.get("MANAGORGID")));	
					actionForm.setMANAGORGNAME(CommonTools.null2String((String)map.get("MANAGORGNAME")));	
					actionForm.setTEL(CommonTools.null2String((String)map.get("TEL")));				
					actionForm.setMOBIL(CommonTools.null2String((String)map.get("MOBIL")));			
					actionForm.setNOTES(CommonTools.null2String((String)map.get("NOTES")));
					String ip = (String)CommonTools.null2String((String)map.get("IP"));
					if(!"".equals(ip)){
						String ip1 = ip.substring(0, ip.indexOf(".",1));
						ip = ip.substring(ip1.length()+ 1);
						String ip2 = ip.substring(0, ip.indexOf(".",1));
						ip = ip.substring(ip2.length()+ 1);
						String ip3 = ip.substring(0, ip.indexOf(".",1));
						ip = ip.substring(ip3.length()+ 1);
						String ip4 = ip.substring(0, ip.indexOf(".",1));
						ip = ip.substring(ip4.length()+ 1);
						actionForm.setIP1(ip1);
						actionForm.setIP2(ip2);
						actionForm.setIP3(ip3);
						actionForm.setIP4(ip4);
						actionForm.setIP5(ip);
						actionForm.setFlag(strTrue);
					}else{
						actionForm.setIP1("");
						actionForm.setIP2("");
						actionForm.setIP3("");
						actionForm.setIP4("");
						actionForm.setIP5("");
						actionForm.setFlag(strFalse);
					}
				 }
				return mapping.findForward(strDetail);	
			}

			if(Constants.DELETE.equals(operationType)){
				try{
					dao.deleteByUserKey(CommonTools.null2String(actionForm.getUSERID()));
					actionForm.setType(strType);
					operationType=strQuery;
				    actionForm.setErrorFlag("");
				}catch(Exception e){
					actionForm.setErrorFlag("");
					e.printStackTrace();
				}    
			}
			
			if(Constants.BACK.equals(operationType)){
				actionForm.setType(strType);
				return mapping.findForward(strList);
			}
			
			if(strRole.equals(operationType)){ 
				actionForm.setArrayID(null);
				actionForm.setLeftList(dao.selectByUserLift(actionForm.getUSERID()));
				actionForm.setRightList(dao.selectByUserRight(actionForm.getUSERID()));
				return mapping.findForward(strAddRole);	
			}

			if(strQuery.equals(operationType)){
				actionForm.setList(dao.selectByUser(actionForm.getORGID()));
				actionForm.setORGID(actionForm.getORGID());	
				actionForm.setORGNAME(actionForm.getORGNAME());
				actionForm.setArrayID(null);
				actionForm.setErrorFlag(actionForm.getErrorFlag());
				return mapping.findForward(strList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null) dao.disConnect();
		}		
		return mapping.findForward(strList);
	}
	
  	public String getNameById(String id,OrgUserDAO dao){
		String name;
		StringBuffer sql = new StringBuffer();
		try {
			sql.append("SELECT NAME FROM T_S_ORG WHERE ORGID='"+id+"'");
			name=dao.queryString(sql.toString());			
		}catch (Exception e){
			e.printStackTrace();
			return "";
		}
		return name;
	}
}
